package day11.gec.preparestatement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class StatementLogin {
	public static void main(String[] args) {
		String name = "xx";
		//String password = "123";
		
		String password = "' or '1'='1";//恒等式  1=1 2=2 3=3
		String sql = "select * from t_user where name = '" + name + "' and password = '" + password + "'";
		System.out.println("sql:" + sql);
		//输入用户名和密码参数后，数据库接受到的完整sql语句将是这种形式：
		//select * from t where username = 'admin' and password = '123' ddos
		
		try(
			Connection conn = DriverManager.getConnection("jdbc:mysql:///hrm", "root", "root");
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			) {
			while(rs.next()) {
				System.out.println(rs.getInt("id") + ":" + rs.getString("name") + ":" + rs.getString("password"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
				
				
		//但是如果用户输入的pwd参数是：' or '1'='1, 则数据库收到的SQL语句将是：
		//select * from t where username = 'admin' and password = '' or '1'='1’;
		//此SQL语句的where条件将永远为true。即用户不需要输入正确的帐号密码，也能登录。
	}
}
